<?php

class FieldService extends BaseService{

    /**
     * 增加字段
     * @param [int]     $modelId    模型id
     * @param [string]  $name       字段名
     * @param [string]  $type       字段类型
     * @param [string]  $displayName字段显示名
     * @return [array] 异常信息
     */
    public static function add($data){
        try{
            $model = Model::find($data['modelId']);
            if(!isset($model)){
                throw new ModelNoFoundException;
            }

            Schema::table($model['table_name'], function($table) use ($data) {
                $table->$data['type']($data['name']);
            });

            $field = new Field;
            $field->name = $data['name'];
            $field->type = $data['type'];
            $field->display_name = $data['displayName'];
            $field->model_id = $data['modelId'];
            $field->save();
        }catch(ModelNoFoundException $e){
            throw $e;
        }catch(Exception $e){
            throw new FieldAddException;
        }
        return self::getResult('增加字段成功');

    }

    /**
     * @param  [int]    $id         字段id
     * @param  [string] $name       字段名
     * @param  [string] $type       字段类型
     * @param  [string] $displayName字段显示名
     * @return 异常信息
     */
    public static function update($data){
        try{
            $field = Field::find($data['id']);
            if(!isset($field)){
                throw new FieldNoFoundException;
            }

            $model = Model::find($field['model_id']);
            if(!isset($model)){
                throw new ModelNoFoundException;
            }

            if($field['type'] !== $data['type']){

                Schema::table($model['table_name'], function($table) use ($field){
                    $table->dropColumn($field['name']);
                });

                if($field['name'] !== $data['name']){
                    Schema::table($model['table_name'], function($table) use ($data){
                        $table->$data['type']($data['name']);
                    });
                }else{
                    Schema::table($model['table_name'], function($table) use ($data, $field){
                        $table->$data['type']($field['name']);
                    });
                }
            }else{
                if($field['name'] !== $data['name']){
                    Schema::table($model['table_name'], function($table) use ($data, $field){
                        $table->renameColumn($field['name'], $data['name']);
                    });
                }
            }

            $field->name = $data['name'];
            $field->type = $data['type'];
            $field->display_name = $data['displayName'];
            $field->model_id = $field['model_id'];
            $field->save();

        }catch(ModelNoFoundException $e){
            return $e;
        }catch(FieldNoFoundException $e){
            throw $e;
        }
        return self::getResult('更新字段成功');
    }

    /**
     * @param  [int]  $id
     * @return 异常信息
     */
    public static function delete($id){
        $field = Field::find($id);
        if(!isset($field)){
            throw new FieldNoFoundException;
        }
        $field->delete();
        return self::getResult('删除字段成功');
    }
}